package com.sevennotes.classtable.ui.pages.components

import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.forEachGesture
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material.Divider
import androidx.compose.material.OutlinedButton
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.*
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.launch

@Composable
fun WheelSelector(
    modifier: Modifier = Modifier,
    tittle: String = "",
    lists: List<List<String>>,
    selected: (Int) -> Int = { 0 },
    onCancel: (() -> Unit)? = null,
    onConfirm: ((Array<Int>) -> Unit)? = null
) {
    val states = mutableListOf<LazyListState>()
    for (i in (lists.indices)) {
        states.add(rememberLazyListState(selected(i)))
    }
    Column(modifier = modifier.fillMaxWidth()) {
        TopBar(
            modifier = Modifier.height(50.dp),
            tittle = tittle,
            onCancel = onCancel
        ) {
            val indexArray = Array<Int>(lists.size) { 0 }
            states.forEachIndexed { index, state ->
                indexArray[index] = state.firstVisibleItemIndex
            }
            if (onConfirm != null) {
                onConfirm(indexArray)
            }
        }
        Divider(Modifier.height(1.dp).background(Color.Black))
        Row {
            lists.forEachIndexed { index, list ->
                SelectList(modifier = Modifier.weight(1f), items = list, state = states[index])
            }
        }
    }
}

@Composable
private fun SelectList(
    modifier: Modifier = Modifier,
    items: List<String>,
    state: LazyListState = rememberLazyListState()
) {
    val itemHeight = 50
    val scope = rememberCoroutineScope()
    fun isSelecteChange(index: Int): Boolean {
        return !(!(index == state.firstVisibleItemIndex && state.firstVisibleItemScrollOffset < itemHeight / 2) && !(state.firstVisibleItemScrollOffset > itemHeight / 2 && index == state.firstVisibleItemIndex + 1))
    }
    fun scrollToFirst() {
        val scrollItemIndex = if (state.firstVisibleItemScrollOffset * 1.5 > itemHeight)
            state.firstVisibleItemIndex + 1
        else
            state.firstVisibleItemIndex
        scope.launch {
            state.animateScrollToItem(scrollItemIndex)
        }
    }
    LazyColumn(
        modifier = modifier
            .onGloballyPositioned {
                scrollToFirst()
            }
            .pointerInput(Unit) {
                forEachGesture {
                    awaitPointerEventScope {
                        var action: PointerEvent
                        do {
                            action = awaitPointerEvent(PointerEventPass.Initial)
                        } while (!action.changes[0].changedToUpIgnoreConsumed())
                        scrollToFirst()
                    }
                }
            }
            .height((5 * itemHeight).dp),
        state = state
    ) {
        items(2) {
            Spacer(modifier = Modifier.height(itemHeight.dp))
        }
        items(items.size) {
            Box(
                modifier = Modifier
                    .height(itemHeight.dp)
                    .fillMaxWidth(),
                contentAlignment = Alignment.Center
            ) {
                Text(
                    text = items[it],
                    color = if (isSelecteChange(it)) Color.Black else Color.Gray
                )
            }
        }
        items(2) {
            Spacer(modifier = Modifier.height(itemHeight.dp))
        }
    }
}

@Composable
private fun TopBar(
    modifier: Modifier = Modifier,
    tittle: String,
    onCancel: (() -> Unit)? = null,
    onConfirm: () -> Unit
) {
    Row(
        modifier = modifier
            .padding(vertical = 5.dp)
            .fillMaxWidth(),
        verticalAlignment = Alignment.CenterVertically
    ) {
        OutlinedButton(
            modifier = Modifier
                .weight(1f)
                .wrapContentWidth(Alignment.Start),
            onClick = { if (onCancel != null) onCancel() }
        ) {
            Text(text = "取消", color = Color.Black)
        }
        Text(text = tittle)
        OutlinedButton(
            modifier = Modifier
                .weight(1f)
                .wrapContentWidth(Alignment.End),
            onClick = { onConfirm() }
        ) {
            Text(text = "确定", color = Color.Black)
        }
    }
}